﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using Microsoft.WindowsAzure.StorageClient;

namespace LCA.Community.DAL.Model
{
    /// <summary>
    /// This class represents an answer of a user question.
    /// 
    /// RowKey = The answer ID
    /// PartitionKey = The question ID
    /// </summary>
    public class Answer : BasicDataModel
    {
        #region Fields
        private IList<AnswerVote> votes; 
        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="Answer"/> class.
        /// </summary>
        public Answer()
        {

        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Answer"/> class.
        /// </summary>
        /// <param name="question">The question.</param>
        /// <param name="answerID">The answer ID.</param>
        public Answer(string question, string body, string author)
        {
            this.RowKey = Guid.NewGuid().ToString();
            this.PartitionKey = question;
            Body = body;
            Author = author;
        }

        /// <summary>
        /// Gets the votes.
        /// </summary>
        /// <returns>The votes.</returns>
        public int GetVotes()
        {
            if(votes == null)
            {
                //votes = LCAComMVCWeb.Libraries.LCAComRepositoryFactory.Repository.GetAnswerVotes(RowKey);
            }
            return (from vote in votes where vote.Value == 1 select vote).Count() - (from vote in votes where vote.Value == -1 select vote).Count();
        }

        /// <summary>
        /// Gets or sets the question.
        /// </summary>
        /// <value>The question.</value>
        public string Question
        {
            get
            {
                return PartitionKey;
            }
            set
            {
                PartitionKey = value;
            }
        }
        /// <summary>
        /// Gets or sets the author.
        /// </summary>
        /// <value>The author.</value>
        [Required(ErrorMessage = "Debe introducir un autor.")]
        public string Author { get; set; }
        /// <summary>
        /// Gets or sets the body.
        /// </summary>
        /// <value>The body.</value>
        [Required(ErrorMessage = "Debe introducir un texto.")]
        public string Body { get; set; }
    }
}